/** DoctorsOrders */
package domain.records;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import domain.doctor.Doctor;
import domain.patient.Patient;

/** 
 * Orders only written by a Doctor 
 * @author jessienewman
 * @version 1.0
 * */
public class DoctorsOrders implements Serializable{
	
	/**
	 * Field id.
	 */
	private Integer id;
	
	/**
	 * Field doctor.
	 */
	private Doctor doctor;
	
	/**
	 * Field patient.
	 */
	private Patient patient;
	
	/**
	 * Field followUp.
	 */
	private String followUp;
	
	/**
	 * Field service.
	 */
	private List<MedicalService> service;
	
	/**
	 * Field prescriptions.
	 */
	private String prescriptions;
	
	/** creates a DoctorsOrders
	 * @param d doctor
	 * @param p patient
	 */
	public DoctorsOrders(Doctor d, Patient p) {
		this(d, p, "", null, "");
	}
	
	/** creates a doctors orders
	 * @param doctor doctor
	 * @param patient patient
	 * @param followUp follow up instructions
	 * @param service services provided during visit
	 * @param prescriptions perscriptions to fill
	 */
	public DoctorsOrders(Doctor doctor, Patient patient, 
			String followUp, List<MedicalService> service, 
			String prescriptions) {
		this.doctor = doctor;
		this.patient = patient;
		this.followUp = followUp;
		this.service = ((service != null) ? service : new ArrayList<MedicalService>());
		this.prescriptions = prescriptions;
	}
	
	public DoctorsOrders(final JSONObject json) {
		populateFromJSON(json);
	}
	
	/**
	 * Method setId.
	 * @param id Integer
	 */
	public void setId(Integer id) {
		this.id = id;
	}
	
	/**
	 * Method getId.
	 * @return Integer
	 */
	public Integer getId() {
		return id;
	}
	
	/**
	 * Method setPrescriptions.
	 * @param script String
	 */
	public void setPrescriptions(String script){
		prescriptions = script;
	}
	
	/**
	 * Method addService.
	 * @param s MedicalService
	 */
	public void addService(MedicalService s){
		
		service.add(s);
	}
	
	/**
	 * Method setServices.
	 * @param ms List<MedicalService>
	 */
	public void setServices(List<MedicalService> ms) {
		service = new ArrayList<MedicalService>(ms);
	}
	
	/**
	 * Method setServices.
	 * @param ms MedicalService[]
	 */
	public void setServices(MedicalService[] ms) {
		service = new ArrayList<MedicalService>(Arrays.asList(ms));
	}
	
	/**
	 * Method addFollowUpInstructions.
	 * @param instr String
	 */
	public void addFollowUpInstructions(String instr){
		followUp = instr;
	}

	/**
	 * Method getDoctor.
	 * @return Doctor
	 */
	public Doctor getDoctor() {
		return doctor;
	}

	/**
	 * Method setDoctor.
	 * @param doctor Doctor
	 */
	public void setDoctor(Doctor doctor) {
		this.doctor = doctor;
	}

	/**
	 * Method getFollowUp.
	 * @return String
	 */
	public String getFollowUp() {
		return followUp;
	}

	/**
	 * Method getService.
	 * @return List<MedicalService>
	 */
	public List<MedicalService> getService() {
		return service;
	}

	/**
	 * Method getPrescriptions.
	 * @return String
	 */
	public String getPrescriptions() {
		return prescriptions;
	}
	
	/**
	 * Method toString.
	 * @return String
	 */
	public String toString() {
			return "Doctors Orders by " + getDoctor().getFullName();
	}
	
	public JSONObject getJSON() {
		final JSONObject json = new JSONObject();
		json.put("doctor", doctor.getJSON());
		json.put("doctorsOrdersId", id);
		json.put("prescriptions", prescriptions);
		
		JSONArray serviceArray = new JSONArray();
		for (MedicalService s : service) {
			serviceArray.put(s.getJSON());
		}
		json.put("services", serviceArray);
		return json;
	}
	
	public void populateFromJSON(final JSONObject json) {
		setDoctor(new Doctor(json.getJSONObject("doctor")));
		setId(json.getInt("doctorsOrdersId"));
		setPrescriptions(json.getString("prescriptions"));
		
		List<MedicalService> services = new ArrayList<MedicalService>();
		JSONArray serviceArray = json.getJSONArray("services");
		
		for (int i = 0; i < serviceArray.length(); i++) {
			services.add(new MedicalService(serviceArray.getJSONObject(i)));
		}
		setServices(services);
	}
}